package com.lzy.cloud.search.client.service.impl;

import com.lzy.cloud.search.client.entity.EsQuery;
import com.lzy.cloud.search.client.service.MatchAllQueryService;
import com.lzy.cloud.search.client.util.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;

/**
 * 最简单的查询，它匹配所有文档
 */
@Service
public class MatchAllQueryServiceImpl implements MatchAllQueryService {

    @Autowired
    private EsClient client;

    @Override
    public SearchResponse matchAllQuerys(EsQuery esQuery)   {
        QueryBuilder qb = matchAllQuery();

        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())//可以是多个index
                .setTypes(esQuery.getTypes())//可以是多个类型
                .setQuery(qb)    // Query 查询条件
                .get();

        return response;

    }

}
